﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using MPMovieCatalog.Util;

namespace MPMovieCatalog.Configuration.Sections
{
    public sealed class GUISection : PropertySection
    {
        [ConfigurationProperty("language")]
        private PropertyElement m_language
        {
            get
            {
                PropertyElement language = (PropertyElement)base["language"];
                return language;
            }

            set
            {
                base["language"] = value;
            }
        }

        [ConfigurationProperty("template")]
        private PropertyElement m_template
        {
            get
            {
                PropertyElement template = (PropertyElement)base["template"];
                return template;
            }

            set
            {
                base["template"] = value;
            }
        }

        [ConfigurationProperty("columns")]
        private PropertyElement m_columns
        {
            get
            {
                PropertyElement columns = (PropertyElement)base["columns"];
                return columns;
            }

            set
            {
                base["columns"] = value;
            }
        }

        public string Language
        {
            get
            {
                return m_language.Value;
            }
            set
            {
                SetProperty("language", value);
            }
        }

        public string Template
        {
            get
            {
                return m_template.Value;
            }
            set
            {
                SetProperty("template", value);
            }
        }

        public ArrayList Columns
        {
            get
            {
                ArrayList columns = new ArrayList();
                if (!string.IsNullOrEmpty(m_columns.Value))
                {
                    foreach (string col in m_columns.Value.Split(';'))
                    {
                        string[] colArray = col.Split(':');
                        if (colArray.Length > 0)
                        {
                            Field field = MPMovieCatalog.Util.Fields.FindFieldByName(colArray[0]);
                            if (field != null)
                            {
                                int width = 0;
                                if (colArray.Length > 1)
                                {
                                    if (!int.TryParse(colArray[1], out width))
                                    {
                                        width = 0;
                                    }
                                }
                                columns.Add(new Column(field, width));
                            }
                        }
                    }
                }

                return columns;
            }
            set
            {
                StringBuilder sb = new StringBuilder();
                foreach (Column column in value)
                {
                    if (sb.Length > 0)
                    {
                        sb.Append(";");
                    }
                    sb.Append(string.Format("{0}:{1}", column.Field.Name, column.Width));
                }
                SetProperty("columns", sb.ToString());
            }
        }
    }
}
